package com.example.sxt_practise.thread;

import java.util.Objects;

/**
 * 1.推荐让线程自己停下来
 * 2. 建议使用一个标志位进行终止
 * @author gaofeng
 * @date 2022年05月25日 22:11
 */
public class ThreadStopTest implements Runnable{

    /**
     * 线程停止标志位
     */
    private boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            System.out.println(Thread.currentThread().getName() + "---> Thread is running...");
        }
    }

    public void stop() {
        this.flag = false ;
    }

    public static void main(String[] args) {
        ThreadStopTest thread = new ThreadStopTest() ;

        new Thread(thread).start();

        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "---> thread is running...");
            if (Objects.equals(50, i)) {
                thread.stop();
                System.out.println(Thread.currentThread().getName() + "---> thread is stop...");

            }
        }
    }
}
